package com.tywj.buscustomerapp.presenter;

import com.amap.api.maps.model.LatLng;
import com.tywj.buscustomerapp.model.MapTicketModel;
import com.tywj.buscustomerapp.model.bean.StationBean;
import com.tywj.buscustomerapp.presenter.contract.MapTicketContract;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MapTicketPresenter implements MapTicketContract.MapTicketPresenter{
    private MapTicketContract.MapTicketModel model;
    private MapTicketContract.MapTicketView view;

    public MapTicketPresenter(MapTicketContract.MapTicketView view) {
        this.view = view;
        model = new MapTicketModel();
    }

    @Override
    public void startLoadStations(final String stationId) {
        Observable.create(new ObservableOnSubscribe<List<StationBean>>() {
            @Override
            public void subscribe(ObservableEmitter<List<StationBean>> e){
                List<StationBean> list = model.loadStation(stationId);
                if (list.size()>0) {
                    e.onNext(list);
                    e.onComplete();
                }else{
                    e.onError(null);
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<StationBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        if (view!=null)
                            view.showLoading();
                    }

                    @Override
                    public void onNext(List<StationBean> s) {
                        if (view!=null) {
                            view.loadedStation(s);
                            view.closeLoading();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view!=null) {
                            view.showMsg("线路加载失败");
                            view.closeLoading();
                        }
                    }

                    @Override
                    public void onComplete() {
                        if (view!=null) {
                            view.closeLoading();
                        }
                    }
                });
    }

    @Override
    public void getBusLocation(final String lineNumber, final String city) {
        Observable.create(new ObservableOnSubscribe<LatLng>() {
            @Override
            public void subscribe(ObservableEmitter<LatLng> e){
                while (true){
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException es) {
                        es.printStackTrace();
                    }
                    LatLng lng = model.getBusLocation(lineNumber,city);
                    if (lng==null){
                        continue;
                    }else{
                        if (lng.latitude==0 && lng.longitude==0){
                            break;
                        }else{
                            e.onNext(lng);
                        }
                    }

                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LatLng>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(LatLng s) {
                        if (view!=null)
                            view.getedLocation(s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {
                        if (view!=null)
                            view.closeLoading();
                    }
                });
    }

    @Override
    public void onDestroy() {
        view = null;
        System.gc();
    }
}
